<script lang="ts" setup>
import Selector from "@/components/Selector.vue";
import Inputter from "@/components/Inputter.vue";
import { ElMessage } from "element-plus";
import instance from '@/utils/request';
import { useAdminStore } from "@/stores/admin";
import router from "@/router/router";
import { ref, onMounted } from "vue";

// 管理员订单管理页面
onMounted(() => {
  init();
});

const admin = useAdminStore();

let order_id = ref("");

let dialogVisible = ref(false);

// 搜索
const order_id_select = ref("");
const order_room_id_select = ref("");
const order_stay_name_select = ref("");
const order_room_type_select = ref("全部");
const order_room_price_select = ref("");
const order_user_id_select = ref("");
const order_customer_name_select = ref("");
const order_customer_sex_select = ref("全部");
const order_customer_phone_select = ref("");
const order_customer_id_select = ref("");
const order_start_time_select = ref("");
const order_end_time_select = ref("");
const order_employee_id_select = ref("");
const order_employee_name_select = ref("");
const order_status_select = ref("全部");

// 类型的下拉框
const order_room_type_options = [
  { value:'全部', label:'全部' },
  { value:'标准间', label:'标准间' },
  { value:'标准大床房', label:'标准大床房' },
  { value:'高级大床房', label:'高级大床房' },
  { value:'商务大床房', label:'商务大床房' },
  { value:'标准双床房', label:'标准双床房' },
  { value:'高级双床房', label:'高级双床房' },
  { value:'商务双床房', label:'商务双床房' },
  { value:'标准家庭房', label:'标准家庭房' },
  { value:'高级家庭房', label:'高级家庭房' }
];

// 性别的下拉框
const order_customer_sex_options = [
  { value:'全部', label:'全部' },
  { value:'男', label:'男' },
  { value:'女', label:'女' }
];

// 状态的下拉框
const order_status_options = [
  { value:'全部', label:'全部' },
  { value:'已退订', label:'已退订' },
  { value:'已退房', label:'已退房' }
];

interface Data {
  order_id: string
  order_room_id: string
  order_stay_name: string
  order_room_type: string
  order_room_price: string
  order_user_id: string
  order_customer_name: string
  order_customer_sex: string
  order_customer_phone: string
  order_customer_id: string
  order_start_time: string
  order_end_time: string
  order_employee_id: string
  order_employee_name: string
  order_status: string
}

// 不同状态不同样式
const rowClassName = ({row, rowIndex,}: {
  row: Data
  rowIndex: number
}) => {
  if (row.order_status === '已退订') {
    return 'color-admin-history-1';
  }
  else if (row.order_status === '已退房') {
    return 'color-admin-history-2';
  }
  return '';
}

// 数据
let tableData = [];

// 条件查询
const filterTableData = ref();
function filter() {
  console.log(tableData);
  filterTableData.value = tableData.filter((data) =>
      (!order_id_select.value || data.order_id.toString().includes(order_id_select.value)) &&
      (!order_room_id_select.value || data.order_room_id.toString().includes(order_room_id_select.value)) &&
      (!order_stay_name_select.value || data.order_stay_name.toString().includes(order_stay_name_select.value)) &&
      (order_room_type_select.value === '全部' || data.order_room_type.includes(order_room_type_select.value)) &&
      (!order_room_price_select.value || data.order_room_price.includes(order_room_price_select.value)) &&
      (!order_user_id_select.value || data.order_user_id.toString().includes(order_user_id_select.value)) &&
      (!order_customer_name_select.value || data.order_customer_name.includes(order_customer_name_select.value)) &&
      (order_customer_sex_select.value === '全部' || data.order_customer_sex.includes(order_customer_sex_select.value)) &&
      (!order_customer_phone_select.value || data.order_customer_phone.includes(order_customer_phone_select.value)) &&
      (!order_customer_id_select.value || data.order_customer_id.includes(order_customer_id_select.value)) &&
      //(!order_start_time_select.value || data.order_start_time.includes(order_start_time_select.value)) &&
      //(!order_end_time_select.value || data.order_end_time.includes(order_end_time_select.value)) &&
      (!order_employee_id_select.value || data.order_employee_id.includes(order_employee_id_select.value)) &&
      (!order_employee_name_select.value || data.order_employee_name.includes(order_employee_name_select.value)) &&
      (order_status_select.value === '全部' || data.order_status.includes(order_status_select.value))
  );
}

function open(id) {
  // 打开确认对话框
  dialogVisible.value = true;
  order_id.value = id.toString();
}

function close() {
  // 关闭确认对话框
  dialogVisible.value = false;
  order_id.value = '';
}

function init() {
  // 初始化
  instance.request({
    url: 'admin/history/init',
    method: 'POST',
    headers: {
      "Admin-Id": admin.admin_id,
      "Admin-Password": admin.admin_password
    }
  })
      .then(function(response) {
        if(response.data.code === "ERROR") {
          ElMessage({
            message: "初始化失败！",
            type: "error"
          });
        }
        if(response.data.code === "SUCCESS") {
          tableData = response.data.data;
          filterTableData.value = tableData;
        }
      })
      .catch(function(error){
        console.log(error);
        if(error.response.request.status === 401) {
          ElMessage({
            message: "无权限！",
            type: "error"
          });
          admin.logout();
          router.push("/admin/index");
          return;
        }
        ElMessage({
          message: "初始化失败！",
          type: "error"
        });
        setTimeout(() => {
          init();
        }, 2000 );
      });
}

function Delete() {
  // 删除历史订单
  instance.request({
    url: 'admin/history/delete',
    method: 'POST',
    data: {
      order_id: order_id.value
    },
    headers: {
      "Admin-Id": admin.admin_id,
      "Admin-Password": admin.admin_password
    }
  })
      .then(function(response) {
        if(response.data.code === "ERROR") {
          ElMessage({
            message: "删除失败！",
            type: "error"
          });
        }
        if(response.data.code === "SUCCESS") {
          ElMessage({
            message: "删除成功！",
            type: "success"
          });
          dialogVisible.value = false;
        }
        init();
      })
      .catch(function(error){
        console.log(error);
        if(error.response.request.status === 401) {
          ElMessage({
            message: "无权限！",
            type: "error"
          });
          admin.logout();
          router.push("/amdin/index");
          return;
        }
        ElMessage({
          message: "删除失败！",
          type: "error"
        });
      });
}
</script>

<template>
  <el-dialog
      v-model="dialogVisible"
      :show-close="false"
      align-center
      width="18%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
  >
    <span>确认删除</span>
    <div class="cancel-confirm">
      <el-button color="#568fc1" plain @click="close">
        <span>取消</span>
      </el-button>

      <el-button color="#568fc1" class="button-color" @click="Delete">
        <span>确认</span>
      </el-button>
    </div>
  </el-dialog>

  <div class="select">
    <span class="select-text">订单号：</span>
    <inputter width="100" v-model="order_id_select"/>

    <span class="select-text">房间号：</span>
    <inputter width="100" v-model="order_room_id_select"/>

    <span class="select-text">实际入住人：</span>
    <inputter width="100" v-model="order_stay_name_select"/>

    <span class="select-text">类型：</span>
    <selector :options="order_room_type_options" width="100" v-model="order_room_type_select"/>

    <span class="select-text">价格：</span>
    <inputter width="100" v-model="order_room_price_select"/>

    <br><br><span class="select-text">用户ID：</span>
    <inputter width="100" v-model="order_user_id_select"/>

    <span class="select-text">用户实名：</span>
    <inputter width="100" v-model="order_customer_name_select"/>

    <span class="select-text">用户性别：</span>
    <selector :options="order_customer_sex_options" width="100" v-model="order_customer_sex_select"/>

    <span class="select-text-long">用户电话号码：</span>
    <inputter width="200" v-model="order_customer_phone_select"/>

    <br><br><span class="select-text">工号：</span>
    <inputter width="100" v-model="order_employee_id_select"/>

    <span class="select-text">负责人：</span>
    <inputter width="100" v-model="order_employee_name_select"/>

    <span class="select-text">状态：</span>
    <selector :options="order_status_options" width="100" v-model="order_status_select"/>

    <!--
    <br><br><span class="select-text">入住时间：</span>
    <inputter width="200" v-model="order_start_time_select"/>

    &emsp;&emsp;<span class="select-text">退房时间：</span>
    <inputter width="200" v-model="order_end_time_select"/>
    -->

    <span class="select-text-long">用户身份证号：</span>
    <inputter width="200" v-model="order_customer_id_select"/>

    &emsp;&emsp;&emsp;
    <el-button color="#568fc1" class="button-color" @click="filter">
      <span>搜索</span>
    </el-button>
  </div>

  <div class="table">
    <el-table
        header-row-style="color: #000000; background-color: #f0f0f0;"
        height="600"
        style="width: 100%"
        :data="filterTableData"
        :row-class-name="rowClassName"
    >
      <el-table-column prop="order_id" label="订单号" width="150"/>
      <el-table-column prop="order_room_id" label="房间号" width="100"/>
      <el-table-column prop="order_stay_name" label="实际入住人" width="150"/>
      <el-table-column prop="order_room_type" label="类型" width="150"/>
      <el-table-column prop="order_room_price" label="价格" width="150"/>
      <el-table-column prop="order_user_id" label="用户ID" width="100"/>
      <el-table-column prop="order_customer_name" label="用户实名" width="150"/>
      <el-table-column prop="order_customer_sex" label="用户性别" width="100"/>
      <el-table-column prop="order_customer_phone" label="用户电话号码" width="150"/>
      <el-table-column prop="order_customer_id" label="用户身份证号" width="180"/>
      <el-table-column prop="order_start_time" label="入住时间" width="180"/>
      <el-table-column prop="order_end_time" label="退房时间" width="180"/>
      <el-table-column prop="order_employee_id" label="工号" width="100"/>
      <el-table-column prop="order_employee_name" label="负责人" width="150"/>
      <el-table-column prop="order_status" label="状态" width="100"/>

      <el-table-column align="right" fixed="right" width="150">
        <template #default="scope">
          <el-button type="danger" plain
                     @click="open(scope.row.order_id)">
            <span>删除</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped>
.select {
  padding-top: 2vh;
  padding-left: 0;
  padding-right: 5vh;
  /* border: 1px solid black; */
}
.table {
  padding: 1vh 5vh 5vh;
  /* border: 1px solid black; */
}
.button-color {
  --el-button-hover-bg-color: rgb(238, 244, 249) !important;
  --el-button-hover-text-color: #568fc1 !important;
  --el-button-text-color: white !important;
}
.cancel-confirm {
  padding-top: 1vh;
  text-align: center;
}
.select-text {
  display: inline-block;
  width: 14vh;
  text-align: right;
}
.select-text-long {
  display: inline-block;
  width: 20vh;
  text-align: right;
}
</style>

<style>
.el-table .color-admin-history-1 {
  --el-table-tr-bg-color: #f0f0f0;
  --el-table-row-hover-bg-color: #f0f0f0;
}
.el-table .color-admin-history-2 {
  --el-table-tr-bg-color: #e4f7fb;
  --el-table-row-hover-bg-color: #e4f7fb;
}
</style>